/**
 * API端点管理服务
 */
import { request } from "@/http/axios"

export interface ApiEndpoint {
  id?: number
  tableId: number
  tableName: string
  path: string
  method: "GET" | "POST" | "PUT" | "DELETE"
  operationType: "CREATE" | "READ" | "UPDATE" | "DELETE" | "LIST"
  description?: string
  requestBody?: any
  responseBody?: any
  enabled: boolean
  createdAt?: string
  updatedAt?: string
}

export interface ApiEndpointRequest {
  tableId: number
  path: string
  method: "GET" | "POST" | "PUT" | "DELETE"
  operationType: "CREATE" | "READ" | "UPDATE" | "DELETE" | "LIST"
  description?: string
  requestBody?: any
  responseBody?: any
  enabled?: boolean
}

export type ApiEndpointListResponse = ApiResponseData<{
  records: ApiEndpoint[]
  total: number
  current: number
  size: number
}>

export type ApiEndpointResponse = ApiResponseData<ApiEndpoint>

export class ApiEndpointService {
  /**
   * 获取API端点列表
   */
  static async getApiEndpoints(params?: {
    current?: number
    size?: number
    tableId?: number
    method?: string
    operationType?: string
    enabled?: boolean
  }): Promise<ApiEndpointListResponse> {
    return request<ApiEndpointListResponse>({
      url: "apiEndpoint/list",
      method: "get",
      params
    })
  }

  /**
   * 获取API端点详情
   */
  static async getApiEndpoint(id: number): Promise<ApiEndpointResponse> {
    return request<ApiEndpointResponse>({
      url: `apiEndpoint/${id}`,
      method: "get"
    })
  }

  /**
   * 创建API端点
   */
  static async createApiEndpoint(data: ApiEndpointRequest): Promise<ApiEndpointResponse> {
    return request<ApiEndpointResponse>({
      url: "apiEndpoint",
      method: "post",
      data
    })
  }

  /**
   * 更新API端点
   */
  static async updateApiEndpoint(id: number, data: Partial<ApiEndpointRequest>): Promise<ApiEndpointResponse> {
    return request<ApiEndpointResponse>({
      url: `apiEndpoint/${id}`,
      method: "put",
      data
    })
  }

  /**
   * 删除API端点
   */
  static async deleteApiEndpoint(id: number): Promise<ApiResponseData<void>> {
    return request<ApiResponseData<void>>({
      url: `apiEndpoint/${id}`,
      method: "delete"
    })
  }

  /**
   * 批量删除API端点
   */
  static async batchDeleteApiEndpoints(ids: number[]): Promise<ApiResponseData<void>> {
    return request<ApiResponseData<void>>({
      url: "apiEndpoint/batch",
      method: "delete",
      data: { ids }
    })
  }

  /**
   * 启用/禁用API端点
   */
  static async toggleApiEndpoint(id: number, enabled: boolean): Promise<ApiResponseData<void>> {
    return request<ApiResponseData<void>>({
      url: `apiEndpoint/${id}/toggle`,
      method: "post",
      data: { enabled }
    })
  }

  /**
   * 自动生成表的CRUD端点
   */
  static async generateCrudEndpoints(tableId: number): Promise<ApiResponseData<{ generatedCount: number }>> {
    return request<ApiResponseData<{ generatedCount: number }>>({
      url: `apiEndpoint/generateCrud/${tableId}`,
      method: "post"
    })
  }

  /**
   * 预览API端点代码
   */
  static async previewEndpointCode(id: number, framework: string): Promise<ApiResponseData<{ code: string }>> {
    return request<ApiResponseData<{ code: string }>>({
      url: `apiEndpoint/${id}/preview`,
      method: "get",
      params: { framework }
    })
  }
}
